<%@page import="com.powroznik.tracker.engine.services.Location"%>
<%@page import="com.powroznik.tracker.engine.services.Device"%>
<%@page import="com.powroznik.tracker.engine.services.PortalServiceProxy"%>
<%@page import="com.powroznik.tracker.engine.services.PortalService"%>
<%@page import="org.apache.commons.lang3.StringUtils"%>

<%@ taglib prefix="aui" uri="http://liferay.com/tld/aui"%>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0"%>

<portlet:defineObjects />

<portlet:renderURL var="viewURL">
	<portlet:param name="jspPage" value="/html/demo1portlet/view.jsp" />
</portlet:renderURL>

<%
	PortalService portalService = new PortalServiceProxy();
	String code1 = renderRequest.getParameter("code1");
	Device device = null;
	Location location = null;
	if (StringUtils.isNotBlank(code1)) {
		device = portalService.getDeviceByCode1(StringUtils
				.upperCase(code1));
		if (device != null) {
			location = portalService.getLastKnownLocation(device);
		}
	}

	if (StringUtils.isBlank(code1)) {
%>

<%
	}
	if (StringUtils.isNotBlank(code1) && device == null) {
%>

<div class="portlet-msg-error">Response: Device not found</div>

<%
	}
	if (device != null && location == null) {
%>

<div class="portlet-msg-error">Response: No location</div>

<%
	}
%>

<aui:form action="<%=viewURL%>" method="post">
	<aui:fieldset>
		<aui:input label="code" name="code1" type="text" />
		<aui:button-row>
			<aui:button type="submit" value="get" />
		</aui:button-row>
	</aui:fieldset>
</aui:form>

<%
	if (location != null) {
%>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<div id="map_canvas_1"></div>

<script>
	$(document).ready(
		function() {
			$("#map_canvas_1").height(2 * $("#map_canvas_1").width() / 3);

			var mapOptions = {
				center : new google.maps.LatLng(<%=location.getLatitude()%>, <%=location.getLongitude()%>),
				mapTypeId : google.maps.MapTypeId.TERRAIN,
				zoom : 15
			};

			var map = new google.maps.Map(document.getElementById("map_canvas_1"), mapOptions);

			var markerOptions = {
				position : new google.maps.LatLng(<%=location.getLatitude()%>, <%=location.getLongitude()%>),
				map : map,
				title : "<%=location.getTime().getTime()%>"
			};

			var marker = new google.maps.Marker(markerOptions);
		});
</script>

<%
	}
%>